package com.teamwork.module01.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwork.common.entity.module01.Subject;
import com.teamwork.common.entity.module01.Teacher;
import com.teamwork.common.result.Result;
import com.teamwork.module01.mapper.TeacherMapper;
import com.teamwork.module01.service.TeacherService;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    /**
     * 根据名称获取班级信息
     * @param name
     * @return
     */
    @Override
    public Result getByName(String name) {
        LambdaQueryWrapper<Teacher> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getTeacherName,name).or().like(Teacher::getTeacherName,name);
        Teacher one = this.getOne(wrapper);
        if(Objects.isNull(one))
            return Result.failure("该教师不存在");

        return Result.success(one);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Result getPage(Integer page, Integer size) {
        Page<Teacher> teacherPage =new Page<>(page,size);
        this.page(teacherPage);
        return Result.success(teacherPage);
    }

    /**
     * 添加或者修改
     * @param teacher
     * @return
     */
    @Override
    public Result InsertOrUpdate(Teacher teacher) {
        if(teacher.getId()!=null || !teacher.getId().equals("")){
            //修改
            boolean b = this.updateById(teacher);
            if(!b) return Result.failure("修改失败");
            return Result.success("修改成功");
        }
        //添加
        boolean sb = this.save(teacher);
        if(!sb) return Result.failure("添加失败");
        return Result.success("添加成功");

    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @Override
    public Result deleteById(Integer id) {
        return null;
    }

    /**
     * id数组批量删除
     * @param ids
     * @return
     */
    @Override
    public Result batchDelete(List<Integer> ids) {
        for (Integer id : ids) {
            boolean b = this.removeById(id);
            if(!b) return Result.failure("删除失败");
        }
        return Result.success("删除成功");
    }
}
